<?php


namespace app\controller;

use app\model\Buy;
use app\model\Document as DocumentModel;
use app\model\User as UserModel;
use think\exception\ValidateException;
use think\Request;
use think\facade\Session;
use app\validate\Document as DocumentValidate;

class Document extends Base
{
    public function upload(Request $request){
        $username=Session::get('username');
        // 获取表单上传文件 例如上传了001.jpg
        $file= $request->file('document');
        $data=$request->param();
        $filename=$file->getOriginalName();
        $size=$file->getSize();
        $s=$size;
        $count=0;
        while(1){
            $s/=1024;
            if($s<=0){
                break;
            }
            $count++;
        }
        switch ($count){
            case 0:$i="B";break;
            case 1:$i="KB";break;
            case 2:$i="GB";break;
        }
        $size=round($size/pow(1024,$count),2).$i;
        try {
            validate(DocumentValidate::class)
                ->check((array)$file);
        } catch (ValidateException $e) {
            return $this->createApi([], $e->getError(), 400);
        }
        // 上传到本地服务器
        if($username){
            $savename = \think\facade\Filesystem::disk('public')->putFileAs( 'document', $file,$filename);
            $document=DocumentModel::create([
               'dStorage'=> 'storage/'.str_replace('\\','/',$savename),
                'dUsername'=>$username,
                'dName'=>$data['name'],
                'd_extension'=>$file->getOriginalExtension(),
                'limit_money'=>$data['limit'],
                'description'=>$data['description'],
                'size'=>$size
            ])->toArray();
            $user=UserModel::where('userName',$username)->find();
            $user->userMoney+=($data['limit']*0.8);
            if($document&&$user->save()){
                return $this->createApi($document, '上传成功', 200);
            }else{
                return $this->createApi([], '上传失败', 204);
            }
        }else{
                return $this->createApi([], '你还未登录', 204);
        }
    }
    public function search(Request $request){
        $data=$request->param();
        $document=DocumentModel::where('dName','like','%'.$data['filename'].'%')
            ->order('create_time','desc')
            ->field('description,dUsername,dName,limit_money,create_time')
            ->select()->toArray();
        if($document){
            return $this->createApi($document, '搜索成功', 200);
        }else{
            return $this->createApi([], '搜索失败', 204);
        }
    }
    public function free(Request $request){
        $data=$request->param();
        $document=DocumentModel::where('dName','like','%'.$data['filename'].'%')->where('limit_money',0)->order('create_time','desc')
            ->field('description,dUsername,dName,limit_money,create_time')
            ->select()->toArray();
        if($document){
            return $this->createApi($document, '免费文件查询', 200);
        }else{
            return $this->createApi([], '搜索失败', 204);
        }
    }
    public function download(Request $request){//你确定要下载吗
        $username=Session::get('username');
        $data=$request->param();
        $user=UserModel::where('userName',$username)->find();
        $document=DocumentModel::where('dId',$data['id'])->find();
        if($document->limit_money>$user->userMoney){
            return $this->createApi([], '你不够钱', 204);
        }else{
            $user->userMoney-=$document->limit_money;
            Buy::create([
                'username'=>$username,
                'document_name'=>$document->dName,
                'document_add'=>$document->dStorage
            ]);
            if($user->save()){
                return $this->createApi($document->dStorage,'这是文件地址', 200);
            }
        }
    }
    public function get_extension() {
//        $file=request()->file('document');
//        $size=$file->getSize();
//        $s=$size;
//        $count=0;
//        while(1){
//            $s/=1024;
//            if($s<=0){
//                break;
//            }
//            $count++;
//        }
//        switch ($count){
//            case 0:$i="B";break;
//            case 1:$i="KB";break;
//            case 2:$i="GB";break;
//        }
//        return $size/pow(1024,$count).$i;
        $d=DocumentModel::where('dUsername','fhnyy')->field('dName')->select()->toArray();
        $result=[];
        foreach ($d as $name=>$value){
            $result[]=$value;
        }
        return $this->createApi($result,'success',200);
    }
    public function searchBuy(){
        $username=Session::get('username');
        $buy=Buy::where('username',$username)->field('document_name')->select()->toArray();
        $result=[];
        foreach ($buy as $name){
            $document=DocumentModel::where('dName',$name['document_name'])->find();
            $result[]=$document;
        }
        if($result){
            return $this->createApi($result,'success',200);
        }else{
            return  $this->createApi([],'failed',204);
        }
    }
}